<template>
    <div class="main">
        <el-radio-group v-model="tabPosition" @change="changePage">
            <el-radio-button label="0">未学习</el-radio-button>
            <el-radio-button label="1">学习中</el-radio-button>
            <el-radio-button label="2">已学完</el-radio-button>
        </el-radio-group>
        <!-- 搜索框 中/英文名称 -->
        <el-input
            placeholder="输入课程名称"
            style="width:250px;margin-left:50px"
            @keyup.enter.native="onSubmit"
            v-model="title"
        >
            <i
                slot="suffix"
                class="el-icon-search"
                style="margin-top:10px;cursor:pointer"
                @click="onSubmit"
            ></i>
        </el-input>
        <el-row :gutter="24" style="margin-top:20px;">
            <el-col
                :span="4.5"
                v-for="(item, index) in dataList"
                :key="index"
                offset="1"
                style="margin-top:20px;padding-left:0;padding-right:0"
            >
                <el-card :body-style="{ padding: '0px' }">
                    <img style="width:240px;height:320px" :src="item.cover" class="image" />
                    <div>
                        <span v-if="item.title && item.title.length > 12">
                            <el-tooltip
                                class="item"
                                effect="light"
                                :content="item.title"
                                placement="bottom"
                            >
                                <h3
                                    style="padding-left:12px;padding-top:12px;font-weight:300; width:220px;"
                                >
                                    {{ subLength(item.title) }}
                                </h3>
                            </el-tooltip>
                        </span>
                        <span v-else style="font-weight:300; width:220px;">
                            <h3 style="padding-left:12px;padding-top:12px">{{ item.title }}</h3>
                        </span>
                        <div class="bottom clearfix">
                            <el-progress
                                :percentage="item.learningProgress"
                                color="#409eff"
                            ></el-progress>
                            <el-divider></el-divider>
                            <el-button
                                type="text"
                                class="button"
                                style="text-align:center;font-size:14px"
                                @click="toDetail(item.id, item.fileName, item.learningId)"
                            >
                                <span v-if="tabPosition === '0'">开始学习</span>
                                <span v-if="tabPosition === '1'">继续学习</span>
                                <span v-if="tabPosition === '2'">重新学习</span>
                            </el-button>
                        </div>
                    </div>
                </el-card>
            </el-col>
        </el-row>
    </div>
</template>
<script>
import courseManageApi from 'common/src/api/academicBehaviorNorms/lessonManage/courseManage'
import UTILS from 'common/src/utils/utils'
import { queryDic } from 'common/src/api/dictionary'
import { downloadimage } from 'common/src/api/upload'
export default {
    data() {
        return {
            title: '',
            tabPosition: '0',
            dataList: [],
            id: '',
            queryParm: {
                pageNum_: 1,
                pageSize_: 9999,
                condition: {
                    title: '',
                    learningStatus: 0
                }
            }
        }
    },
    created() {
        this.queryParm.condition.learningStatus = this.tabVal
        this.tabPosition = this.tabVal
        this.getData()
    },
    props: {
        tabVal: {
            type: String,
            default: '0'
        }
    },
    methods: {
        onSubmit() {
            this.getData()
        },
        toDetail(id, fileName, learningId) {
            if(this.tabPosition === '2') {
                    let parm = {
                        id: learningId,
                        progress: 0,
                        status: 1
                    }
                 courseManageApi.updateLearningStatus(parm).then(res => {})
            }

            this.$emit('toDetail', {
                val: 'detail',
                id: id,
                fileName: fileName,
                tabVal: this.tabPosition
            })
        },
        subLength(val) {
            return val = val.substring(0, 12) + "...";
        },
        getData() {
            this.queryParm.condition.learningStatus = this.tabPosition
            this.queryParm.condition.title = this.title
            this.dataList = []
            courseManageApi.getLearningPage(this.queryParm).then(res => {
                if (res.code === 200 && res.data) {
                    res.data.list.forEach(item => {
                        if (item.cover) {
                            downloadimage(item.cover).then(resT => {
                                if (resT && resT.data) {
                                    item.cover = resT.data.base
                                }
                            })
                        }
                        this.dataList.push(item)
                    })
                }
            })
        },
        changePage(val) {
            this.getData()
        }
    }
}
</script>
<style scoped lang="scss">
.main ::v-deep {
    .el-tabs__item {
        font-size: 14px !important;
    }
    .bottom {
        margin-top: 13px;
        line-height: 12px;
        text-align: center;
    }

    .image {
        width: 100%;
        display: block;
    }

    .clearfix:before,
    .clearfix:after {
        display: table;
        content: '';
    }

    .clearfix:after {
        clear: both;
    }
}
</style>